<?php

/**
 * @Project NUKEVIET 3.0
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2010 VINADES.,JSC. All rights reserved
 * @Createdate 3/9/2010 23:25
 */

if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );

if ( ! nv_function_exists( 'nv_news_hit_by_viewing' ) )
{
    function nv_news_hit_by_viewing ()
    {
        global $global_array_cat, $module_info, $module_name, $module_data, $op, $db, $catid, $id, $module_config;
		
		$blockwidth = $module_config[$module_name]['blockwidth'];
		
		if( $op == 'detail' )
		{
			$sql = "SELECT `id`, `catid`, `title`, `alias`, `homeimgfile`, `homeimgthumb`, `homeimgalt` FROM `" . NV_PREFIXLANG . "_" . $module_data . "_" . $catid . "` WHERE `status`=1 AND `inhome`='1' AND `id`!=" . $id . " ORDER BY `hitstotal` DESC LIMIT 0,10";
			
			$result = $db->sql_query( $sql );

			if ( $db->sql_numrows( $result ) )
			{
				$a = 1;
				$xtpl = new XTemplate( "block_hit_by_view.tpl", NV_ROOTDIR . "/themes/" . $module_info['template'] . "/modules/news" );
				
				while( $array_news = $db->sql_fetchrow( $result ) )
				{
					$array_news['link'] = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=" . $global_array_cat[$array_news['catid']]['alias'] . "/" . $array_news['alias'] . "-" . $array_news['id'];
					
					$array_news['imgurl'] = "";
					$array_news['width'] = $blockwidth;
					
					if ( ! empty( $array_news['homeimgthumb'] ) )
					{
						$array_img = array();
						$array_img = explode( "|", $array_news['homeimgthumb'] );
						if ( $array_img[0] != "" and file_exists( NV_ROOTDIR . '/' . NV_FILES_DIR . '/' . $module_name . '/' . $array_img[0] ) )
						{
							$imgurl = NV_BASE_SITEURL . NV_FILES_DIR . '/' . $module_name . '/' . $array_img[0];
							$array_news['imgurl'] = $imgurl;
						}
					}
					elseif ( nv_is_url( $array_news['homeimgfile'] ) )
					{
						$array_news['imgurl'] = $array_news['homeimgfile'];
					}
					
					$xtpl->assign( 'blocknews', $array_news );
					if ( ! empty( $array_news['imgurl'] ) )
					{
						$xtpl->parse( 'main.newloop.imgblock' );
					}
					$xtpl->parse( 'main.newloop' );
					$xtpl->assign('BACKGROUND', ( $a % 2 ) ? 'bg ' : '' );
					++$a;
				}

				$xtpl->parse( 'main' );
				return $xtpl->text( 'main' );
			}
        }
    }
}

if ( defined( 'NV_IS_MOD_NEWS' ) )
{
	$content = nv_news_hit_by_viewing();
}

?>